#ifndef HTTP_SERVER_H
#define HTTP_SERVER_H

#include "../../tcp/code/utility/Noncopyable.h"
#include "../../tcp/code/TcpServer.h"
#include "../../tcp/code/utility/Logger.h"

// 让外部只包含当前.h即可
#include "./HttpServer.h"
#include "./HttpRequest.h"
#include "./HttpContext.h"
#include "./HttpResponse.h"

class HttpRequest;
class HttpResponse;

class HttpServer : Noncopyable
{
public:
    using HttpCallback = std::function<void(const HttpRequest &, HttpResponse *)>; // 请求不变响应变

    // 类似TcpServer的构造方法
    HttpServer(EventLoop *loop,
               const InetAddress &listenAddr,
               TcpServer::ListenOption listenOpt,
               const std::string &threadName);

    void setHttpCallback(const HttpCallback &cb)
    {
        this->httpCallback_ = std::move(cb);
    }

    void start();

private:
    // 静态不依赖对象，才能转为HttpCallback移动给成员变量
    static void defaultHttpCallback(const HttpRequest &request, HttpResponse *response);

    void onConnectionCallback(const TcpConnectionPtr &conn);
    void onReadWriteCallback(const TcpConnectionPtr &conn, Buffer *buffer, Timestamp receiveTime);
    void handleRequest(const TcpConnectionPtr &conn, const HttpRequest &request);

    TcpServer tcpServer_;
    HttpCallback httpCallback_;
};

#endif // HTTP_SERVER_H